﻿<?php
// Error Reporting
error_reporting(E_ALL);

// Check Version
if (version_compare(phpversion(), '5.2.0', '<') == TRUE) 
{
	exit('PHP5.2+ Required');
}

// Register Globals
if (ini_get('register_globals')) {
	ini_set('session.use_cookies', 'On');
	ini_set('session.use_trans_sid', 'Off');
		
	session_set_cookie_params(0, '/');
	session_start();
	
	$globals = array($_REQUEST, $_SESSION, $_SERVER, $_FILES);

	foreach ($globals as $global) {
		foreach(array_keys($global) as $key) {
			unset($$key);
		}
	}
}

// Magic Quotes Fix
if (ini_get('magic_quotes_gpc')) {
	function clean($data) {
   		if (is_array($data)) {
  			foreach ($data as $key => $value) {
    			$data[clean($key)] = clean($value);
  			}
		} else {
  			$data = stripslashes($data);
		}
	
		return $data;
	}			
	
	$_GET = clean($_GET);
	$_POST = clean($_POST);
	$_COOKIE = clean($_COOKIE);
}

// Windows IIS Compatibility  
if (!isset($_SERVER['DOCUMENT_ROOT'])) { 
	if (isset($_SERVER['SCRIPT_FILENAME'])) {
		$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr($_SERVER['SCRIPT_FILENAME'], 0, 0 - strlen($_SERVER['PHP_SELF'])));
	}
}

if (!isset($_SERVER['DOCUMENT_ROOT'])) {
	if (isset($_SERVER['PATH_TRANSLATED'])) {
		$_SERVER['DOCUMENT_ROOT'] = str_replace('\\', '/', substr(str_replace('\\\\', '\\', $_SERVER['PATH_TRANSLATED']), 0, 0 - strlen($_SERVER['PHP_SELF'])));
	}
}

if (!isset($_SERVER['REQUEST_URI'])) { 
	$_SERVER['REQUEST_URI'] = substr($_SERVER['PHP_SELF'], 1); 
	
	if (isset($_SERVER['QUERY_STRING'])) { 
		$_SERVER['REQUEST_URI'] .= '?' . $_SERVER['QUERY_STRING']; 
	} 
}

// Engine
require_once(SYSTEM_DIR . '/engine/Action.php'); 
require_once(SYSTEM_DIR . '/engine/Controller.php');
require_once(SYSTEM_DIR . '/engine/Front.php');
require_once(SYSTEM_DIR . '/engine/Loader.php'); 
require_once(SYSTEM_DIR . '/engine/Model.php');
require_once(SYSTEM_DIR . '/engine/Registry.php');


// Common
require_once(SYSTEM_DIR . '/library/Cache.php');
require_once(SYSTEM_DIR . '/library/Config.php');
require_once(SYSTEM_DIR . '/library/DB.php');
require_once(SYSTEM_DIR . '/library/Document.php');
require_once(SYSTEM_DIR . '/library/Language.php');
require_once(SYSTEM_DIR . '/library/Log.php');
require_once(SYSTEM_DIR . '/library/Request.php');
require_once(SYSTEM_DIR . '/library/Response.php');
require_once(SYSTEM_DIR . '/library/Session.php');
require_once(SYSTEM_DIR . '/library/Template.php');

define('LANGUAGE_DIR', APPLICATION_DIR.'/language');
define('TEMPLATE_DIR', APPLICATION_DIR.'/view/theme');
define('IMAGE_DIR', APPLICATION_DIR.'/image');
define('CACHE_DIR', APPLICATION_DIR.'/cache');




// Error Handler
function error_handler($errno, $errstr, $errfile, $errline) {
	$config = new Config();
	$log = new Log($config->get('config_error_filename'));

	switch ($errno) {
		case E_NOTICE:
		case E_USER_NOTICE:
			$error = 'Notice';
			break;
		case E_WARNING:
		case E_USER_WARNING:
			$error = 'Warning';
			break;
		case E_ERROR:
		case E_USER_ERROR:
			$error = 'Fatal Error';
			break;
		default:
			$error = 'Unknown';
			break;
	}
		
	if ($config->get('config_error_display')) {
		echo '<b>' . $error . '</b>: ' . $errstr . ' in <b>' . $errfile . '</b> on line <b>' . $errline . '</b>';
	}
	
	if ($config->get('config_error_log')) {
		$log->write('PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline);
	}

	return TRUE;
}

// Error Handler
set_error_handler('error_handler');




class Bootstrap
{
	static public function run()
	{
		// Registry
		$registry = new Registry();

		// Loader
		$loader = new Loader($registry);
		$registry->set('load', $loader);

		// Config
		$config =  new Config();
		$registry->set('config', $config);

		// Database 
		$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		$registry->set('db', $db);


		define('HTTP_SERVER', $config->get('config_url'));
		define('HTTP_IMAGE', HTTP_SERVER . 'image/');

		// Log 
		$log = new Log($config->get('config_error_filename'));
		$registry->set('log', $log);

		// Request
		$registry->set('request', new Request());
		 
		// Response
		$response = new Response();
		$response->addHeader('Content-Type: text/html; charset=utf-8');
		$registry->set('response', $response); 

		// Cache
		$registry->set('cache', new Cache());

		// Session
		$registry->set('session', new Session());
			
		// Document
		$registry->set('document', new Document());

		// Language		
		$languages = $config->get('language');
		$languages['directory'] = 'zh_cn';
		$languages['filename'] = 'b';
		$language = new Language($languages['directory']);
		$language->load($languages['filename']);	
		$registry->set('language', $language);

		// Front Controller 
		$controller = new Front($registry);

		// Maintenance Mode
		//$controller->addPreAction(new Action('common/maintenance/check'));

		// SEO URL's
		//$controller->addPreAction(new Action('common/seo_url'));

		// Router
		if (isset($request->get['route'])) 
		{
			$action = new Action($request->get['route']);
		} 
		else 
		{
			$action = new Action('common/home');
		}

		// Dispatch
		$controller->dispatch($action, new Action('error/not_found'));

		// Output
		$response->output();
		
		return true;
	}
}


